package com.xunk.system.controller;

import com.xunk.common.core.utils.poi.ExcelUtil;
import com.xunk.common.core.web.controller.BaseController;
import com.xunk.common.core.web.domain.AjaxResult;
import com.xunk.common.core.web.page.TableDataInfo;
import com.xunk.common.log.annotation.Log;
import com.xunk.common.log.enums.BusinessType;
import com.xunk.system.domain.SysDevice;
import com.xunk.system.factory.SysDeviceServiceFactory;
import com.xunk.system.service.ISysDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 设备Controller
 *
 * @author ruoyi
 * @date 2021-03-17
 */
@RestController
@RequestMapping("/device")
public class SysDeviceController extends BaseController {
    @Autowired
    @Qualifier("sysDeviceServiceImpl")
    private ISysDeviceService sysDeviceService;

    @Autowired
    private SysDeviceServiceFactory deviceServiceFactory;

    /**
     * 查询设备列表
     */
    @PreAuthorize("@ss.hasPermi('buz:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDevice sysDevice) {
        startPage();
        List<SysDevice> list = sysDeviceService.selectSysDeviceList(sysDevice);
        return getDataTable(list);
    }

    /**
     * 导出设备列表
     */
    @PreAuthorize("@ss.hasPermi('buz:device:export')")
    @Log(title = "设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDevice sysDevice) throws IOException {
        List<SysDevice> list = sysDeviceService.selectSysDeviceList(sysDevice);
        ExcelUtil<SysDevice> util = new ExcelUtil<SysDevice>(SysDevice.class);
        util.exportExcel(response, list, "device");
    }

    /**
     * 获取设备详细信息
     * manufacturer字段传mandunDevice或ruyingDevice，对应曼顿设备或如影设备，新增设备厂商可新增实现类
     */
    @PreAuthorize("@ss.hasPermi('buz:device:query')")
    @GetMapping(value = "/getInfo/{deviceSn}")
    public AjaxResult getInfo(@PathVariable("deviceSn") String deviceSn) throws Exception {
        AjaxResult ajaxResult = sysDeviceService.selectSysDeviceById(deviceSn);
        String manufacturer = ((SysDevice) ajaxResult.getData()).getSupplier();
        ISysDeviceService deviceService = deviceServiceFactory.getSysDeviceService(manufacturer);
        return deviceService.selectSysDeviceById(deviceSn);
    }

    /**
     * 新增设备，家庭等信息不能为空
     */
    @PreAuthorize("@ss.hasPermi('buz:device:add')")
    @Log(title = "设备", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDevice sysDevice) {
        return toAjax(sysDeviceService.insertSysDevice(sysDevice));
    }

    /**
     * 修改设备
     */
    @PreAuthorize("@ss.hasPermi('buz:device:edit')")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDevice sysDevice) {
        return toAjax(sysDeviceService.updateSysDevice(sysDevice));
    }

    /**
     * 修改设备
     */
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult consumeEdit(@RequestBody SysDevice sysDevice) {
        return toAjax(sysDeviceService.updateSysDevice(sysDevice));
    }

    /**
     * 删除设备
     */
//    @PreAuthorize("@ss.hasPermi('buz:device:remove')")
    @Log(title = "设备", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deviceSns}")
    public AjaxResult remove(@PathVariable String[] deviceSns) {
        return toAjax(sysDeviceService.deleteSysDeviceByIds(deviceSns));
    }

    @GetMapping("/room")
    public AjaxResult getDeviceInfo(@RequestParam("homeId") Long homeId, @RequestParam(value = "trigger", required = false) String trigger) {
        return AjaxResult.success(sysDeviceService.getDeviceInfo(homeId, trigger));
    }
}
